package com.jmp.api.dto;

import com.jmp.base.dto.MessagePushClientEnum;
import com.jmp.base.dto.MessagePushType;
import com.jmp.push.MessagePushTypeEnum;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Data
@Builder
public class MessagePushDto {
    /**
     * 推送对应端： 0 红木 1 花园
     */
    private MessagePushClientEnum pushClient;

    private MessagePushType type;
    private MessagePushTypeEnum typeEnum;
    private Integer fromType;
    private Integer fromId;


    /**
     * 推送ID类型
     * {@link com.jmp.base.MessageApi}
     */
    private Integer toType;
    /**
     * 推送ID子类型:
     * 1)关注具体类型
     */
    private Integer toChildType;
    private Integer toId;
    private List<Integer> toIds;



    /**
     * 自定义标题
     */
    private String title;
    /**
     * 内容填充
     */
    private String[] fillData;
    /**
     * 跳转参数
     */
    private Object[] pageParams;

    public String getPushTitle() {
        return getContent(getTypeEnum().getName(), new String[]{title});
    }

    public String getPushContent() {
        return getContent(getTypeEnum().getPushContent(), fillData);
    }

    public String getPushPayload(String appId) {
        if ("BtQObyje7RATziSxsnCIk8".equals(appId)) {//crm jxs
            return "/" + getContent(getTypeEnum().getPage(), pageParams);
        } else {
            return getContent(getTypeEnum().getPage2(), pageParams);
        }
    }

    private String getContent(String str, Object[] params) {
        if (Objects.isNull(params) || params.length <= 0) {
            return str;
        } else {
            String formatStr = String.format(str, params);
            return StringUtils.replaceAll(formatStr, "(\r\n|\r|\n|\n\r)", StringUtils.EMPTY);
        }
    }

    public MessagePushTypeEnum getTypeEnum() {
        return Objects.isNull(typeEnum) ? MessagePushTypeEnum.valueOf(type.name()) : typeEnum;
    }
}
